<html>
    <head>
        <style>
            body {
                padding: 100px;
                margin: 0;
            }

            #box {
                width: 100px;
                height: 100px;
                background-color: #0077ff;
            }

            [data-layout-correct="false"] {
                background: #dd1144 !important;
                opacity: 1 !important;
            }
        </style>
    </head>
    <body>
        <div id="root"></div>
        <script type="module" src="/src/imports/optimized-appear.js"></script>
        <script type="module" src="/src/imports/script-assert.js"></script>

        <script type="module">
            const {
                motion,
                startOptimizedAppearAnimation,
                optimizedAppearDataAttribute,
                motionValue,
            } = window.Motion
            const { matchOpacity } = window.Assert
            const root = document.getElementById("root")

            const duration = 0.25
            const delay = 0.5
            const opacity = motionValue(0)
            let opacityHasChanged = false
            opacity.on("render", (v) => {
                if (!opacityHasChanged) {
                    if (v > 0) {
                        showError(
                            document.getElementById("box"),
                            `opacity should not start animating beyond 0 (started at ${v})`
                        )
                    }
                }
                opacityHasChanged = true
            })

            // This is the tree to be rendered "server" and client-side.
            const Component = React.createElement(motion.div, {
                id: "box",
                initial: { opacity: 0 },
                animate: { opacity: 1 },
                transition: { duration, ease: "linear", delay },
                style: { opacity },
                /**
                 * On animation start, check the values we expect to see here
                 */
                onAnimationStart: () => {
                    matchOpacity(document.getElementById("box"), 0)
                    requestAnimationFrame(() => {
                        matchOpacity(document.getElementById("box"), 0)
                    })
                },
                [optimizedAppearDataAttribute]: "a",
            })

            // Emulate server rendering of element
            root.innerHTML = ReactDOMServer.renderToString(Component)

            startOptimizedAppearAnimation(
                document.getElementById("box"),
                "opacity",
                [0, 1],
                {
                    duration: duration * 1000,
                    ease: "linear",
                    delay: delay * 1000,
                },
                (animation) => {
                    // Hydrate root mid-way through delay
                    setTimeout(() => {
                        ReactDOMClient.hydrateRoot(root, Component)

                        const { opacity: initialOpacity } =
                            window.getComputedStyle(box)
                        if (initialOpacity !== "0") {
                            showError(box, `opacity should have animated`)
                        }
                    }, 300)
                }
            )
        </script>
    </body>
</html>
